const config = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buildingArr: [],
    buildingIndex: 0,
    buildingIdArr: 0,
    buildingOrder: '请选择楼号',
    changeBuildingColor: true,
    addName: '',
    addTel: '',
    addressInput: '',
    isChecked: false,
    buildingAddress: '',
    telVal: '',
    nameVal: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.request({
      url: config.buildingUrl,
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: function (res) {
        // console.log(res)
        var buildingArr = [];
        var buildingIdArr = [];
        res.data.forEach((val, index) => {
          buildingArr.push(res.data[index]);
          buildingIdArr.push(res.data[index].id);
        })
        that.setData({
          buildingArr: buildingArr,
          buildingIdArr: buildingIdArr
        })
      }
    });
  },


  bindBuildingChange: function (e) {
    // console.log(this.data.buildingArr)
    // console.log(e)
    let index = e.detail.value;
    this.setData({
      buildingOrder: this.data.buildingArr[index].address_name,
      changeBuildingColor: false,
      // buildingIndex: index,
      buildingAddress: this.data.buildingArr[index],
      buildingId: this.data.buildingIdArr[index]
    })
    // console.log(this.data.buildingAddress)
  },

  addName: function (e) {
    // console.log(e)
    this.setData({
      addName: e.detail.value
    })
  },
  addTel: function (e) {
    // console.log(e)
    this.setData({
      addTel: e.detail.value
    })
  },

  addressInput: function (e) {
    // console.log(e)
    this.setData({
      addressInput: e.detail.value
    })
  },

  switchChange: function (e) {
    // console.log(e);
    this.setData({
      isChecked: e.detail.value
    })
  },

  saveAddress: function () {
    let addName = this.data.addName;
    let addTel = this.data.addTel;
    let buildingId = this.data.buildingId;
    let buildingAddress = this.data.buildingAddress;
    let addressInput = this.data.addressInput;
    let isChecked = this.data.isChecked;
    if (addName == '') {
      wx.showToast({
        title: '请填写收件人姓名',
        icon: 'none'
      })
    } else if (addTel == '') {
      wx.showToast({
        title: '请填写联系电话',
        icon: 'none'
      })
    } else if (buildingAddress == '') {
      wx.showToast({
        title: '请选择楼号',
        icon: 'none'
      })
    } else if (addressInput == '') {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none'
      })
    } else {
      wx.request({
        url: config.addAddress,
        method: 'POST',
        data: {
          name: addName,
          tel: addTel,
          building_id: buildingId,
          send_address: addressInput,
          status: isChecked ? 1 : 0
        },
        header: {
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        success: (res) => {
          // console.log(res)
          if (res.data == 'success') {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 1500,
              success: () => {
                setTimeout(() => {
                  wx.navigateBack({
                    url: '/pages/address/address',
                  })
                }, 1500)
              }
            })
          }
        }
      })
    }
    //console.log(`${addName}--${addTel}--${buildingId}--${addressInput}--${isChecked}`)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

})